package com.genius.dingding.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

import org.springframework.util.ResourceUtils;

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;

import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * 导出为pdf
 * @author spf
 *
 */
public class ExportPDFUtil {
	/**
	 * HTML 转 PDF
	 * @param content html内容
	 * @param outPath           输出pdf路径
	 * @return 是否创建成功
	 */
	public static boolean html2Pdf(String content, String outPath) {
	    try {
	        Document document = new Document();
	        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outPath));
	        document.open();
	        XMLWorkerHelper.getInstance().parseXHtml(writer, document,
	                new ByteArrayInputStream(content.getBytes()), null, Charset.forName("UTF-8"));
	        document.close();
	    } catch (Exception e) {
	        System.out.println("生成模板内容失败"+e.fillInStackTrace());
	        return false;
	    }
	    return true;
	}
	/**
	 * HTML 转 PDF
	 * @param content html内容
	 * @return PDF字节数组
	 */
	public static byte[] html2Pdf(String content) {
	    ByteArrayOutputStream outputStream = null;
	    try {
	        Document document = new Document();
	        outputStream = new ByteArrayOutputStream();
	        PdfWriter writer = PdfWriter.getInstance(document, outputStream);
	        document.open();
	        XMLWorkerHelper.getInstance().parseXHtml(writer, document,
	                new ByteArrayInputStream(content.getBytes()), null, Charset.forName("UTF-8"));
	        document.close();
	    } catch (Exception e) {
	        System.out.println("------生成pdf失败-------");
	    }
	    return outputStream.toByteArray();
	}
	/**
     * 获取模板内容
     * @param templateDirectory 模板文件夹
     * @param templateName      模板文件名
     * @param paramMap          模板参数
     * @return
     * @throws Exception
     */
    private static String getTemplateContent(String templateDirectory, String templateName, Map<String, Object> paramMap) throws Exception {
        Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
        try {
            configuration.setDirectoryForTemplateLoading(new File(templateDirectory));
        } catch (Exception e) {
            System.out.println("-- exception --");
        }

        Writer out = new StringWriter();
        Template template = configuration.getTemplate(templateName,"UTF-8");
        template.process(paramMap, out);
        out.flush();
        out.close();
        return out.toString();
    }
	public static void main(String[] args) throws Exception {
	    Map<String, Object> paramMap = new HashMap<>();
	    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
	    paramMap.put("date_time", dateTimeFormatter.format(LocalDateTime.now()));
	    paramMap.put("date", dateTimeFormatter.format(LocalDateTime.now()).substring(0, 10));
	    String outPath = "D:\\A.pdf";
//	    String templateDirectory = "D:\\";
	    String templateDirectory = ResourceUtils.getURL("classpath:").getPath()+"genius/pdfmuban";
	    String templateContent = getTemplateContent(templateDirectory, "pandian_jieguo_pdf.html", paramMap);
	    html2Pdf(templateContent, outPath);
	    
	}
}
